package db;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class Coupons {

	// set connection to DBHelper
	DBHelper helper = new DBHelper();
	
	private PreparedStatement getCouponsDataStatement;
	private PreparedStatement getCouponStatement;
	private PreparedStatement addCouponStatement;
	private PreparedStatement updateCouponStatement;
	private PreparedStatement deleteCouponStatement;
	
	// Declaring user data variables
	// coupon name
	private String couponName;

	// coupon id
	private int couponId;

	// coupon point value
	private int pointValue;

	// coupon DollarOffValue
	private Float dollarOffValue;

	// coupon BeginDatetime
	private Date beginDatetime;

	// coupon EndDatetime
	private Date endDatetime;

	// coupon active status
	private int active;

	// coupon birthdayPromo status
	private int birthdayPromo;

	/**
	 * Creates coupon data object that contains the following
	 * 
	 * @param couponName
	 * @param couponId
	 * @param PointValue
	 * @param DollarOffValue
	 * @param BeginDatetime
	 * @param EndDatetime
	 * @param Active
	 * @param birthdayPromo
	 */

	/**
	 * Constructor
	 */
	public Coupons(String couponName, int couponId, int pointValue,
			Float dollarOffValue, Date beginDatetime, Date endDatetime,
			int active, int birthdayPromo) {
		this.couponName = couponName;
		this.couponId = couponId;
		this.pointValue = pointValue;
		this.dollarOffValue = dollarOffValue;
		this.beginDatetime = beginDatetime;
		this.endDatetime = endDatetime;
		this.active = active;
		this.birthdayPromo = birthdayPromo;

	}

	/**
	 * Constructor
	 */
	public Coupons() {
	};

	/**
	 * Adds or edit a coupon
	 * 
	 * @param insertUpdate
	 *            1=update 0=insert
	 */
	public int addEditCoupons(int couponId, int insertUpdate, String Name,
			int PointValue, int DollarOffValue, String BeginDatetime,
			String EndDatetime, int Active, int CreatedBy, int birthdayPromo) {
		int completed = 0;
		ResultSet rs = null;
		Connection conn = helper.connection();

		try {
			
			addCouponStatement = conn.prepareStatement(DBHelper.addCouponStatement);
			updateCouponStatement = conn.prepareStatement(DBHelper.updateCouponStatement);
			
			if (insertUpdate == 0) {
				addCouponStatement.setString(1, Name);
				addCouponStatement.setInt(2, PointValue);
				addCouponStatement.setInt(3, DollarOffValue);
				addCouponStatement.setString(4,
						db.FormatAndValidation.convertDate(BeginDatetime));
				addCouponStatement.setString(5,
						db.FormatAndValidation.convertDate(EndDatetime));
				addCouponStatement.setInt(6, Active);
				addCouponStatement.setInt(7, CreatedBy);
				addCouponStatement.setInt(8, birthdayPromo);

				addCouponStatement.executeUpdate();
			} else {

				updateCouponStatement.setString(1, Name);
				updateCouponStatement.setInt(2, PointValue);
				updateCouponStatement.setInt(3, DollarOffValue);
				updateCouponStatement.setString(4,
						db.FormatAndValidation.convertDate(BeginDatetime));
				updateCouponStatement.setString(5,
						db.FormatAndValidation.convertDate(EndDatetime));
				updateCouponStatement.setInt(6, Active);
				updateCouponStatement.setInt(7, CreatedBy);
				updateCouponStatement.setInt(8, birthdayPromo);

				updateCouponStatement.setInt(9, couponId);

				updateCouponStatement.executeUpdate();
			}

			System.out.println(Name);
			System.out.println(PointValue);
			System.out.println(DollarOffValue);
			System.out.println(BeginDatetime);
			System.out.println(EndDatetime);
			System.out.println(Active);
			System.out.println(CreatedBy);
			System.out.println(couponId);

		}

		catch (Exception e) {

			System.out.println("Error in addEditCoupons: "
					+ e.getClass().getName() + ":" + e.getMessage());
			completed = 10;
		} finally {
			DBHelper.disconnect(rs, addCouponStatement, conn);
			DBHelper.disconnect(rs, updateCouponStatement, conn);
		}
		return completed;
	}

	/** Returns the individual coupon info data */
	public ArrayList<Coupons> getCouponsinfo(int couponID) {
		ArrayList<Coupons> couponInfoData = new ArrayList<Coupons>();
		ResultSet rs = null;
		Connection conn = helper.connection();
		
		try {
			
			getCouponStatement = conn.prepareStatement(DBHelper.getCouponStatement);
			
			System.out.println("start query");
			getCouponStatement.setInt(1, couponID);

			rs = getCouponStatement.executeQuery();
			System.out.println("display rewards ");
			while (rs.next()) {
				System.out.println("display query" + rs.getInt("PointValue"));

				Coupons newCouponData = new Coupons(rs.getString("Name"),
						rs.getInt("idCoupons"), rs.getInt("PointValue"),
						rs.getFloat("DollarOffValue"),
						rs.getDate("BeginDatetime"), rs.getDate("EndDatetime"),
						rs.getInt("Active"), rs.getInt("birthdayPromo"));
				couponInfoData.add(newCouponData);
			}
		}

		catch (Exception e) {
			System.out.println("Error in getCouponStatement: "
					+ e.getClass().getName() + ":" + e.getMessage());
		} finally {
			DBHelper.disconnect(rs, getCouponStatement, conn);
		}
		System.out.println("end query");
		return couponInfoData;
	}

	/** Returns the coupons info data */
	public ArrayList<Coupons> getCouponsinfo() {
		ResultSet rs = null;
		Connection conn = helper.connection();
		ArrayList<Coupons> couponInfoData = new ArrayList<Coupons>();
		try {


			getCouponsDataStatement = conn.prepareStatement(DBHelper.getCouponsDataStatement);
			
			rs = getCouponsDataStatement.executeQuery();
			System.out.println("display rewards ");
			while (rs.next()) {
				System.out.println("display query" + rs.getInt("PointValue"));

				Coupons newCouponData = new Coupons(rs.getString("Name"),
						rs.getInt("idCoupons"), rs.getInt("PointValue"),
						rs.getFloat("DollarOffValue"),
						rs.getDate("BeginDatetime"), rs.getDate("EndDatetime"),
						rs.getInt("Active"), rs.getInt("birthdayPromo"));
				couponInfoData.add(newCouponData);
			}
			rs.close();
		}

		catch (Exception e) {
			System.out.println("Error in getRewardsDataStatement: "
					+ e.getClass().getName() + ":" + e.getMessage());
		} finally {
			DBHelper.disconnect(rs, getCouponsDataStatement, conn);
		}
		System.out.println("end query");
		return couponInfoData;
	}

	/** Returns the all coupons info data */
	public ArrayList<Coupons> getAllCouponsinfo() {
		ResultSet rs = null;
		Connection conn = helper.connection();
		ArrayList<Coupons> couponInfoData = new ArrayList<Coupons>();
		try {
			
			getCouponsDataStatement = conn.prepareStatement(DBHelper.getCouponsDataStatement);
			
			rs = getCouponsDataStatement.executeQuery();
			System.out.println("display rewards ");
			while (rs.next()) {
				System.out.println("display query" + rs.getInt("PointValue"));

				Coupons newCouponData = new Coupons(rs.getString("Name"),
						rs.getInt("idCoupons"), rs.getInt("PointValue"),
						rs.getFloat("DollarOffValue"),
						rs.getDate("BeginDatetime"), rs.getDate("EndDatetime"),
						rs.getInt("Active"), rs.getInt("birthdayPromo"));
				couponInfoData.add(newCouponData);
			}
			rs.close();
		}

		catch (Exception e) {
			System.out.println("Error in getRewardsDataStatement: "
					+ e.getClass().getName() + ":" + e.getMessage());
		} finally {
			DBHelper.disconnect(rs, getCouponsDataStatement, conn);
		}
		System.out.println("end query");
		return couponInfoData;
	}

	/** Returns the delete's coupons info data */
	public int removeCoupon(int couponID) {
		int validate = 0;
		ResultSet rs = null;
		Connection conn = helper.connection();
		
		try {

			deleteCouponStatement = conn.prepareStatement(DBHelper.deleteCouponStatement);
			
			deleteCouponStatement.setInt(1, couponID);

			deleteCouponStatement.executeUpdate();

			System.out.println(validate);

		}

		catch (Exception e) {

			System.out.println("Error in deleteCouponStatement: "
					+ e.getClass().getName() + ":" + e.getMessage());
			validate = 10;
		} finally {
			DBHelper.disconnect(rs, deleteCouponStatement, conn);
		}
		return validate;
	}

	public String getCouponName() {
		return couponName;
	}

	public void setCouponName(String couponName) {
		this.couponName = couponName;
	}

	public int getCouponId() {
		return couponId;
	}

	public void setCouponId(int couponId) {
		this.couponId = couponId;
	}

	public int getPointValue() {
		return pointValue;
	}

	public void setPointValue(int pointValue) {
		this.pointValue = pointValue;
	}

	public Float getDollarOffValue() {
		return dollarOffValue;
	}

	public void setDollarOffValue(Float dollarOffValue) {
		this.dollarOffValue = dollarOffValue;
	}

	public Date getBeginDatetime() {
		return beginDatetime;
	}

	public void setBeginDatetime(Date beginDatetime) {
		this.beginDatetime = beginDatetime;
	}

	public Date getEndDatetime() {
		return endDatetime;
	}

	public void setEndDatetime(Date endDatetime) {
		this.endDatetime = endDatetime;
	}

	public int getActive() {
		return active;
	}

	public void setActive(int active) {
		this.active = active;
	}

	public int getBirthdayPromo() {
		return birthdayPromo;
	}

	public void setBirthdayPromo(int birthdayPromo) {
		this.birthdayPromo = birthdayPromo;
	}
}
